<?php
require("libraries/student.php");
class Type extends Student{
    public function __construct(){
        parent::__construct();
        $this->load->model("mtype");
        $this->load->library("string");
    }
    public function index(){
        $config['base_url'] = base_url().'admin/type/index/';
        $config['total_rows'] = $this->mtype->count_all();
        $config['per_page'] = 10;
        $config['uri_segment'] = 4;
        $config['next_link'] = "Next";
        $config['prev_link'] = "Prev";
        $config['first_link'] = "First";
        $config['last_link'] = "Last";
        $this->load->library("pagination");
        $this->pagination->initialize($config);
        $start = $this->uri->segment(4);
		$data['act'] = 4;
        $data['title']="Holiday types";
        $data['data']['info']= $this->mtype->listall($config['per_page'],$start);
        $data['template'] = "type/list";
        $this->load->view("layout",$data);
    }
    public function add(){
        $this->load->helper(array("form","date"));
        $this->load->library("form_validation");
        $data['error'] = "";
        $data['data'] = "";
        $data['title']="Holiday types";
        $data['template'] = "type/add";
		$data['act'] = 4;
        if($this->input->post("ok") != ""){
            $this->form_validation->set_rules("title","title","min_length[5]|callback_check_news");
            if($this->form_validation->run() == FALSE){
                $this->load->view("layout",$data);
            }else{
				$url = str_replace(' ', '-',strtolower($this->string->replace($this->input->post('name'))));
                $db = array(
                    "name"=>$this->input->post("name"),
					"rewrite" => $url,
					"keywords" => $this->input->post("keywords"),
					"descripti" => $this->input->post("descripti"),
                    "order_by"=> $this->input->post("order_by"),
                    "description" => $this->input->post("description"),
                    "sale" => $this->input->post("sale"),
                    "sale_content" => $this->input->post("sale_content"),
                    "status"=>1,
                    "created_time"=>time(),
                );
                $config['upload_path'] = './uploads/holiday';
                $config['allowed_types'] = 'gif|jpg|jpeg|png';
                $config['max_size']	= '2000';
                $config['max_width']  = '1400';
                $config['max_height']  = '1400';
                if($_FILES['icon']['name'] != NULL){
                    $this->load->library('upload',$config);
                    if(!$this->upload->do_upload("icon")){
                        $data = array('error' => $this->upload->display_errors());
                        $data['data'] = "";
                        $data['title']="Holiday types";
                        $data['template'] = "type/add";
						$data['act'] = 4;
                        $this->load->view("layout",$data);
                        return FALSE;
                    }else{
                        $data = $this->upload->data();
                        $db['icon'] = $data['file_name'];
                    }
                }
                if($_FILES['icon2']['name'] != NULL){
                    $this->load->library('upload',$config);
                    if(!$this->upload->do_upload("icon2")){
                        $data = array('error' => $this->upload->display_errors());
                        $data['data'] = "";
                        $data['title']="Holiday types";
                        $data['template'] = "type/add";
                        $data['act'] = 4;
                        $this->load->view("layout",$data);
                        return FALSE;
                    }else{
                        $data = $this->upload->data();
                        $db['icon2'] = $data['file_name'];
                    }
                }
                if($_FILES['logo']['name'] != NULL){
                    $this->load->library('upload',$config);
                    if(!$this->upload->do_upload("logo")){
                        $data = array('error' => $this->upload->display_errors());
                        $data['data'] = "";
                        $data['title']="Holiday types";
                        $data['template'] = "type/add";
                        $this->load->view("layout",$data);
                        return FALSE;
                    }else{
                        $data = $this->upload->data();
                        $db['logo'] = $data['file_name'];
                    }
                }
                $this->mtype->add($db);
                redirect(base_url().'admin/type/index');
                //redirect(base_url()."admin/news/index");
            }
        }else{
            $this->load->view("layout",$data);
        }
    }

    public function update(){
        $id = $this->uri->segment(4);
        $this->load->helper(array("form","date"));
        $this->load->library("form_validation");
        $data['error'] = "";
        $data['title']="Holiday types";
        $data['template'] = "type/edit";
        $data['data'] = "";
		$data['act'] = 4;
        $data['get'] = $this->mtype->get_data($id);
        if($this->input->post("ok") != ""){
            $this->form_validation->set_rules("title","title","min_length[5]|callback_check_news");
            if($this->form_validation->run() == FALSE){
                $this->load->view("layout",$data);
            }else{
				$url = str_replace(' ', '-',strtolower($this->string->replace($this->input->post('name'))));
                $db = array(
                    "name"=>$this->input->post("name"),
					"rewrite" => $url,
					"keywords" => $this->input->post("keywords"),
					"descripti" => $this->input->post("descripti"),
                    "order_by"=>$this->input->post("order_by"),
                    "description" => $this->input->post("description"),
                    "sale" => $this->input->post("sale"),
                    "sale_content" => $this->input->post("sale_content"),
                    "edit_time"=>time(),
                );
                $config['upload_path'] = './uploads/holiday';
                $config['allowed_types'] = 'gif|jpg|jpeg|png';
                $config['max_size']	= '2000';
                $config['max_width']  = '1400';
                $config['max_height']  = '1400';
                if($_FILES['icon']['name'] != NULL){
                    $this->load->library('upload',$config);
                    if(!$this->upload->do_upload("icon")){
                        $data = array('error' => $this->upload->display_errors());
                        $data['data'] = "";
						$data['act'] = 4;
                        $data['title']="Holiday types";
                        $data['template'] = "type/add";
                        $this->load->view("layout",$data);
                        return FALSE;
                    }else{
                        $data = $this->upload->data();
                        $db['icon'] = $data['file_name'];
                    }
                }
                if($_FILES['icon2']['name'] != NULL){
                    $this->load->library('upload',$config);
                    if(!$this->upload->do_upload("icon2")){
                        $data = array('error' => $this->upload->display_errors());
                        $data['data'] = "";
                        $data['title']="Holiday types";
                        $data['template'] = "type/add";
                        $data['act'] = 4;
                        $this->load->view("layout",$data);
                        return FALSE;
                    }else{
                        $data = $this->upload->data();
                        $db['icon2'] = $data['file_name'];
                    }
                }
                if($_FILES['logo']['name'] != NULL){
                    $this->load->library('upload',$config);
                    if(!$this->upload->do_upload("logo")){
                        $data = array('error' => $this->upload->display_errors());
                        $data['data'] = "";
                        $data['title']="Holiday types";
                        $data['template'] = "type/add";
                        $this->load->view("layout",$data);
                        return FALSE;
                    }else{
                        $data = $this->upload->data();
                        $db['logo'] = $data['file_name'];
                    }
                }
                $this->mtype->update($db,$id);
                redirect(base_url().'admin/type/index');
            }
        }else{
            $this->load->view("layout",$data);
        }
    }

    public function del(){
        $id = $this->uri->segment(4);
        $this->mtype->del($id);
        redirect(base_url().'admin/type/index');
    }
    public function active(){
        $id   	= $this->uri->segment(4);
        $status	= $this->uri->segment(5);
        $db = array(
            "status" => $status
        );
        $this->mtype->update_status($db,$id);
        redirect(base_url().'admin/type/');
    }
    public function createThumbnail($parth,$fileName,$width,$height){
        $this->load->library('image_lib');
        //$this->load->helper('thumbnail_helper');
        $config['image_library'] = 'gd2';
        $config['source_image'] = $parth.'/'.$fileName;
        $config['new_image'] = $parth.'/thumb/'.$fileName;
        $config['create_thumb'] = TRUE;
        $config['maintain_ratio'] = TRUE;
        $config['thumb_marker'] = FALSE;
        $config['width'] = $width;
        $config['height'] = $height;
        $this->image_lib->initialize($config);
        $this->image_lib->resize();
        $this->image_lib->clear();
    }
}